package cn.hsa.xxx.bookOrder.controller;


import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import cn.hsa.xxx.api.dto.CommodityDTO;
import cn.hsa.xxx.api.service.DiscountService;
import cn.hsa.xxx.api.service.PlaceOrderService;
import io.swagger.annotations.Api;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/web/placeOrder")
@RestController
@Slf4j
@Api(tags = "下订单")
public class BookOrderController {

    @Resource(name = "discount")
    private DiscountService discountService;

    @Resource(name = "placeOrder")
    private PlaceOrderService placeOrderService;

    @PutMapping("/setDiscount")
    public WrapperResponse<String> redisSet() {
        discountService.setDiscount(60);
        discountService.setIdOfType(60);
        return WrapperResponse.success("优惠券存入redis成功");
    }

    @PutMapping("/getPreCalculated")
    public WrapperResponse<Pair<Float, Float>> preCalculated(@RequestBody List<CommodityDTO> commodityDTOList) {
        Pair<Float, Float> result = placeOrderService.getPreCalculated(commodityDTOList);
        return WrapperResponse.success(result);
    }

    @PutMapping("/placeOrder")
    public WrapperResponse<Boolean> placeOrder(@RequestBody List<CommodityDTO> commodityDTOList, String description) throws Exception {
        Boolean result = placeOrderService.addPlaceOrderList(commodityDTOList, description);
        return WrapperResponse.success(result);
    }
}
